<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleAdd2">添加门店</el-button>
    </div>

    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">门店负责人：</span>
        <el-input placeholder="请输入负责人" v-model="searchParams.shop_fzr_name" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">门店负责人手机号：</span>
        <el-input placeholder="请输入门店负责人手机号" v-model="searchParams.shop_fzr_tel" @keyup.enter.native="handleSearch"  oninput="value=value.replace(/[^\d]/g,'')"
                  clearable></el-input>
      </div>
      <div class="task-one__item" v-if="!area_id">
        <span class="task-one__label">大区：</span>
        <el-select v-model="searchParams.area_id" placeholder="请选择所属大区" clearable>
          <el-option :label="item.area_title" :value="item.area_id" v-for="(item, index) in selectOptions.area_id" :key="index"/>
        </el-select>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">省/市/区：</span>
        <elui-china-area-dht v-model="searchRegion" @change="handleSearchRegionChange" clearable></elui-china-area-dht>
      </div>
      <div class="task-one__btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="shop_title" label="门店名称"></el-table-column>
      <el-table-column prop="shop_fzr_name" label="门店负责人">
        <template #default="{ row }">
          <div>{{ row.shop_fzr_name }}</div>
          <div>{{ row.shop_fzr_tel }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="addr" label="门店地址">
        <template #default="{ row }">
          <div>{{ row.province }}-{{ row.city }}-{{ row.district }}</div>
          <div>{{ row.addr }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="steward_num" label="门店管家"></el-table-column>
      <el-table-column prop="area_title" label="所属大区"></el-table-column>
      <el-table-column prop="is_stop" label="门店状态">
        <template #default="{ row }">
          <el-text v-if="row.is_stop==1" type="info">停用</el-text>
          <el-text v-if="row.is_stop==2" type="success">正常</el-text>
        </template>
      </el-table-column>
      <el-table-column prop="status_label" label="审核状态">
        <template #default="{ row }">
          <div>
            <el-text v-if="row.status==1" type="warning">{{ row.status_label }}</el-text>
            <el-text v-if="row.status==2" type="success">{{ row.status_label }}</el-text>
            <el-text v-if="row.status==3" type="danger">{{ row.status_label }}</el-text>
          </div>
          <div v-if="row.check_cause">
            审核意见：{{ row.check_cause }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="170" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
          <el-button link type="primary" @click="handleCheck(row)" v-if="!area_id">审核</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row)">
            <template #reference>
              <el-button link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px"
               :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px">
        <el-form-item label="门店名称" prop="shop_title" required>
          <el-input v-model.trim="dialogForm.shop_title" clearable placeholder="请输入门店名称"/>
        </el-form-item>
        <el-form-item label="门店负责人" prop="shop_fzr_name" required>
          <el-input v-model.trim="dialogForm.shop_fzr_name" clearable placeholder="请输入门店负责人"/>
        </el-form-item>
        <el-form-item label="负责人手机号" prop="shop_fzr_tel" required>
          <el-input v-model.trim="dialogForm.shop_fzr_tel" clearable placeholder="请输入负责人手机号"  oninput="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>
        <el-form-item label="所属大区" prop="area_id" required v-if="!area_id">
          <el-select v-model="dialogForm.area_id" placeholder="请选择所属大区">
            <el-option :label="item.area_title" :value="item.area_id" v-for="(item, index) in selectOptions.area_id" :key="index"/>
          </el-select>
        </el-form-item>
        <el-form-item label="区域" prop="region" required>
          <elui-china-area-dht v-model="dialogForm.region" @change="handleRegionChange"></elui-china-area-dht>
        </el-form-item>
        <el-form-item label="详细地址" prop="addr" required>
          <el-input v-model.trim="dialogForm.addr" clearable placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="是否停用" prop="is_stop">
          <el-switch v-model="dialogForm.is_stop" inline-prompt size="large" :active-value="1"
                     :inactive-value="2" active-text="是" inactive-text="否" />
        </el-form-item>
        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm2(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog v-model="checkDialogShow" title="审核" width="660px"
               :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeCheckDialog">
      <el-form ref="checkDialogFormRef" :model="checkDialogForm" :rules="checkDialogFormRules" label-width="120px">
        <el-form-item label="审核结果" prop="status" required>
          <el-radio-group v-model="checkDialogForm.status">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="3">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="check_cause" required>
          <el-input v-model.trim="checkDialogForm.check_cause" clearable placeholder="请输入审核意见" :rows="2" type="textarea"/>
        </el-form-item>
        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="checkDialogParams.loading" @click="submitCheckDialogForm(checkDialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(checkDialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRoute,
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"
import {EluiChinaAreaDht} from "elui-china-area-dht";
import chinaAreaData from "china-area-data";

const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  resetDialogForm,
  submitDialogForm,
  closeDialog,
  handleAction
} = listMixins()

dialogForm.shop_title = '';
dialogForm.shop_fzr_name = '';
dialogForm.shop_fzr_tel = '';
dialogForm.province = '';
dialogForm.province_code = '';
dialogForm.city = '';
dialogForm.city_code = '';
dialogForm.district = '';
dialogForm.district_code = '';
dialogForm.addr = '';
dialogForm.is_stop = 2;
dialogForm.region = [];

const router = useRouter()
const route = useRoute()
const area_id = route.query.area_id || ''
searchParams.area_id = area_id

if(area_id){
  apiPath.value = 'admin/area/shop/index';
}else{
  apiPath.value = 'admin/shop/shop/index';
}
dialogParams.addApiPath = 'admin/shop/shop/create';
dialogParams.editApiPath = 'admin/shop/shop/update';

const searchRegion = reactive([])
const handleSearchRegionChange =(e)=>{
  if(!e){
    searchParams.province_code = ''
    searchParams.city_code = ''
    searchParams.district_code = ''

  }else{
    searchParams.province_code = e[0]
    searchParams.city_code = e[1]
    searchParams.district_code = e[2]
  }
}

const checkDialogShow = ref(false)
const checkDialogForm = reactive({
  shop_id:'',
  status:2,
  check_cause:''
})
const checkDialogFormRef = ref(null)
const checkDialogParams = reactive({
  loading:false
})
onMounted(() => {
  getData()
  getAreaIdSelect()
})

const dialogFormRules = reactive({
  shop_title: [{
    required: true,
    message: "请输入门店名称",
    trigger: "blur"
  }],
  shop_fzr_name: [{
    required: true,
    message: "请输入门店负责人",
    trigger: "blur"
  }],
  shop_fzr_tel: [{
    required: true,
    message: "请输入门店负责人手机号",
    trigger: "blur"
  },{
    validator: proxy.$util.validPhone,
    trigger: "blur"
  }],
  region: [{
    required: true,
    message: "请选择区域",
    trigger: "change"
  }],
})
if(!area_id){
  dialogForm.area_id = '';
  dialogFormRules.area_id = [{
    required: true,
    message: "请选择大区",
    trigger: "change"
  }]
}
const checkDialogFormRules = reactive({
  status: [{
    required: true,
    message: "请选择审核结果",
    trigger: "change"
  }],
  check_cause: [{
    required: true,
    message: "请输入审核意见",
    trigger: "blur"
  }]
})
const handleAdd2 = () =>{
  if(area_id){
    handleAdd({area_id:area_id})
  }else{
    handleAdd()
  }
}
const handleCheck = (row) => {
  checkDialogShow.value = true
  checkDialogForm.shop_id = row.shop_id
  checkDialogForm.status = 2
  checkDialogForm.check_cause = ''
}
const closeCheckDialog = ()=>{
  checkDialogShow.value = false
  resetDialogForm(checkDialogFormRef.value)
}
const submitCheckDialogForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      checkDialogParams.loading = true
      let data = checkDialogForm;
      let url = 'admin/shop/shop/doCheck';
      proxy.$axios.post(url, data)
          .then((res) => {
            checkDialogParams.loading = false
            if (res.data.code == 200) {
              closeCheckDialog()
              proxy.$message({
                message: `成功`,
                type: "success"
              })
              getData()
            } else {
              proxy.$message({
                message: res.data.msg,
                type: "error"
              })
            }
          })
          .catch((err) => {
            checkDialogParams.loading = false
          })
    }
  })
}



const handleEdit = (row) => {
  let data = {
    shop_id:row.shop_id
  }
  if(area_id){
    data.area_id = row.area_id
  }
  dialogShow.value = true
  dialogParams.type = 'edit'
  dialogParams.data = data
  setTimeout(() => {
    for (let key in dialogForm) {
      dialogForm[key] = row[key]
    }
    dialogForm.region = [];
    dialogForm.region.push(row.province_code.toString())
    dialogForm.region.push(row.city_code.toString())
    dialogForm.region.push(row.district_code.toString())
  }, 50)
}
const handleRegionChange = (e) => {
  dialogForm.province = chinaAreaData['86'][e[0]]
  dialogForm.province_code = e[0]
  dialogForm.city = chinaAreaData[e[0]][e[1]]
  dialogForm.city_code = e[1]
  dialogForm.district = chinaAreaData[e[1]][e[2]]
  dialogForm.district_code = e[2]
}
const submitDialogForm2 = (formEl) => {
  let params = JSON.parse(JSON.stringify(dialogForm));
  delete params.region
  submitDialogForm(formEl, params)
}

const handleDelete = (row) => {
  handleAction('admin/shop/shop/delete', {
    shop_id: row.shop_id
  })
}
const selectOptions = reactive({
  area_id:[]
})
const getAreaIdSelect = ()=>{
  proxy.$axios
      .get("common/dataSelect/areaSelect")
      .then((res) => {
        if (res.data.code == 200) {
          selectOptions.area_id = res.data.data
        } else {
          proxy.$message({
            message: res.data.msg,
            type: "error"
          })
        }
      })
      .catch((err) => {
      })
}
</script>

<style lang="less">

</style>